//
//  BaseTabBarViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/3.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "BaseNavigationViewController.h"
#import "HomeViewController.h"
#import "MineViewController.h"
#import "DesignCenterViewController.h"

#import "PaperBoardOrderViewController.h"
#import "AutobiographyViewController.h"
#import "ApplySceneViewController.h"
#import "PaperBoxOrderViewController.h"
#import "PackagingExcipientViewController.h"
#import "OrderViewController.h"
#import "SolutionViewController.h"

@interface BaseTabBarViewController (){
    NSInteger index;//上一次点击的索引
}

@property (nonatomic, strong) UIView *quickNavView;

@end

@implementation BaseTabBarViewController

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    [self removeSubBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self removeSubBtn];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBar.backgroundColor = [UIColor clearColor];
    index = 100;
    [self createSubController];
    [self removeSubBtn];
    [self createTabBarButton];
}

#pragma mark - tabbar
- (void)removeSubBtn {
    for (UIView *view in self.tabBar.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([view isKindOfClass:class]) {
            [view removeFromSuperview];
        }
    }
}

- (void)createSubController {
    NSMutableArray *aryControllers = [NSMutableArray array];
    
    BaseNavigationViewController *homeNav = [[BaseNavigationViewController alloc] initWithRootViewController:[[HomeViewController alloc] init]];
    [aryControllers addObject:homeNav];
    
    BaseNavigationViewController *navNav = [[BaseNavigationViewController alloc] initWithRootViewController:[[HomeViewController alloc] init]];
    [aryControllers addObject:navNav];
    
    BaseNavigationViewController *disignCenterNav = [[BaseNavigationViewController alloc] initWithRootViewController:[[DesignCenterViewController alloc] init]];
    [aryControllers addObject:disignCenterNav];
    
    BaseNavigationViewController *mineNav = [[BaseNavigationViewController alloc] initWithRootViewController:[[MineViewController alloc] init]];
    [aryControllers addObject:mineNav];
    
    self.viewControllers = aryControllers;
}

- (void)createTabBarButton {
    NSArray *nameArray = @[@"首页",@"快速导航",@"设计中心",@"我的"];
    NSArray *imageArray = @[@"首页灰色",@"快速导航",@"设计中心灰色",@"我的灰色"];
    NSArray *selectedImageArray = @[@"首页",@"快速导航",@"设计中心",@"我的"];
    
    CGFloat itemWidth = SCREEN_WIDTH / nameArray.count;
    for (int i = 0; i < nameArray.count; i++) {
        UIButton *tabBarButton = [[UIButton alloc] initWithFrame:CGRectMake(i * itemWidth, 0, itemWidth, 49)];
        [tabBarButton setTitle:nameArray[i] forState:UIControlStateNormal];
        [tabBarButton setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [tabBarButton setImage:[UIImage imageNamed:selectedImageArray[i]] forState:UIControlStateSelected];
        tabBarButton.tag = 100 + i;
        tabBarButton.titleLabel.font = [UIFont systemFontOfSize:10];
        tabBarButton.backgroundColor = [UIColor whiteColor];
        [tabBarButton setTitleColor:ThemeColor forState:UIControlStateSelected];
        [tabBarButton setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
        
        CGSize imgViewSize,titleSize,btnSize;
        UIEdgeInsets imageViewEdge,titleEdge;
        CGFloat heightSpace = 10.0f;
        
        //设置按钮内边距
        imgViewSize = tabBarButton.imageView.bounds.size;
        titleSize = tabBarButton.titleLabel.bounds.size;
        btnSize = tabBarButton.bounds.size;
        imageViewEdge = UIEdgeInsetsMake(0,0.0, btnSize.height -imgViewSize.height - heightSpace, - titleSize.width);
        titleEdge = UIEdgeInsetsMake(32, - imgViewSize.width, 0.0, 0.0);
        [tabBarButton setImageEdgeInsets:imageViewEdge];
        [tabBarButton setTitleEdgeInsets:titleEdge];
        
        if (i == 0) {
            tabBarButton.selected = YES;
        }
        [tabBarButton addTarget:self action:@selector(tabBarAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:tabBarButton];
    }
}

- (void)tabBarAction:(UIButton *)button {
    if (button.tag == 101) {
        if (!_quickNavView) {
            [self createQuickNavView];
        }
        _quickNavView.hidden = NO;
        if (_quickNavView.frame.origin.y == SCREEN_HEIGHT - TAB_BAR_HEIGHT) {
            [UIView animateWithDuration:0.3 animations:^{
                self.quickNavView.frame = CGRectMake(ScreenX375(73), SCREEN_HEIGHT - TAB_BAR_HEIGHT - ScreenX375(266), ScreenX375(131), ScreenX375(266));
                self.quickNavView.alpha = 1;
            }];
        }else {
            [UIView animateWithDuration:0.3 animations:^{
                self.quickNavView.frame = CGRectMake(ScreenX375(73), SCREEN_HEIGHT - TAB_BAR_HEIGHT, ScreenX375(131), ScreenX375(266));
                self.quickNavView.alpha = 0;
            }];
        }
    }else {
        _quickNavView.hidden = YES;
        UIButton *lastBtn = (UIButton *)[self.tabBar viewWithTag:index];
        lastBtn.selected = NO;
        button.selected = YES;
        
        index = button.tag;
        
        self.selectedIndex = button.tag - 100;
        self.tabBar.hidden = NO;
    }
}

- (void)createQuickNavView {
    _quickNavView = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(73), SCREEN_HEIGHT - TAB_BAR_HEIGHT, ScreenX375(131), ScreenX375(266))];
    _quickNavView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    _quickNavView.layer.cornerRadius = 4;
    _quickNavView.clipsToBounds = YES;
    _quickNavView.alpha = 0;
    [kKeyWindow addSubview:_quickNavView];
    
    NSArray *nameArr = @[@"纸箱订购",@"纸板订购",@"其他产品",@"个性化定制",@"帮我下单",@"解决方案"];
    CGFloat height = ScreenX375(266) /nameArr.count;
    for (int i = 0; i < nameArr.count; i++) {
        UIButton *quickBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, i * height, ScreenX375(131), height)];
        [quickBtn setTitle:nameArr[i] forState:UIControlStateNormal];
        [quickBtn setTitleColor:REXADECIMALCOLOR(@"#333333") forState:UIControlStateNormal];
        quickBtn.titleLabel.font = Kfont(14);
        quickBtn.tag = 1000 + i;
        [quickBtn addTarget:self action:@selector(quickNav:) forControlEvents:UIControlEventTouchUpInside];
        [_quickNavView addSubview:quickBtn];
        
        if (i != 0) {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(17), 0, ScreenX375(97), 0.5)];
            line.backgroundColor = LineColor;
            [_quickNavView addSubview:line];
        }
    }
}

- (void)quickNav:(UIButton *)button {
    _quickNavView.hidden = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.quickNavView.frame = CGRectMake(ScreenX375(73), SCREEN_HEIGHT - TAB_BAR_HEIGHT, ScreenX375(131), ScreenX375(266));
        self.quickNavView.alpha = 0;
    }];
    
    if (button.tag == 1001) {
        PaperBoardOrderViewController *vc = [[PaperBoardOrderViewController alloc] init];
        [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
    }else if (button.tag == 1003) {
//        AutobiographyViewController *vc = [[AutobiographyViewController alloc] init];
        ApplySceneViewController *vc = [[ApplySceneViewController alloc] init];
        [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
    }else if (button.tag == 1005){
        SolutionViewController *vc = [[SolutionViewController alloc] init];
        [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
    }else if (button.tag == 1000) {
        PaperBoxOrderViewController *vc = [[PaperBoxOrderViewController alloc] init];
        [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
    }else if (button.tag == 1002) {
        PackagingExcipientViewController *vc = [[PackagingExcipientViewController alloc] init];
        [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
    }else if (button.tag == 1004) {
        OrderViewController *vc = [[OrderViewController alloc] init];
        [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
